package com.htstar.ovms.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htstar.ovms.common.core.util.R;
import com.htstar.ovms.common.log.annotation.SysLog;
import com.htstar.ovms.admin.api.entity.AppUpdate;
import com.htstar.ovms.admin.service.AppUpdateService;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.core.spi.service.ServiceFactory;
import org.springframework.objenesis.instantiator.android.Android10Instantiator;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * app更新版本
 *
 * @author htxk
 * @date 2020-12-15 17:44:35
 */
@RestController
@AllArgsConstructor
@RequestMapping("/appupdate")
@Api(value = "appupdate", tags = "app更新版本管理")
@Slf4j
public class AppUpdateController {

    private final AppUpdateService appUpdateService;
    private static final String ANDROID = "Android";
    private static final String IPHONE = "iPhone";
    private static final String IPAD = "iPad";
    private static final String IPOD = "iPod";
    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param appUpdate app更新版本
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getAppUpdatePage(Page page, AppUpdate appUpdate) {
        return R.ok(appUpdateService.page(page, Wrappers.query(appUpdate)));
    }


    /**
     * 通过id查询app更新版本
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(appUpdateService.getById(id));
    }

    /**
     * 查询最大idapp更新版本信息
     *
     * @param appType 因为前端类型传的null  后端进行不接受 但是还是得带上apptype与前端匹配
     * @return R
     */
    @ApiOperation(value = "查询最新版本", notes = "查询最新版本")
    @GetMapping("/getMaxApp")
    //@ApiImplicitParam(name = "appType", value = "APP版本类型", required = true, paramType="path", dataType = "Long" ,defaultValue = "1")
    public R getOneMaxAppVersion( HttpServletRequest request) {
        LambdaQueryWrapper<AppUpdate> wrapper = new LambdaQueryWrapper<>();
        String agent = request.getHeader("User-Agent");
        //安卓手机
        if(agent.contains(AppUpdateController.ANDROID)) {
            wrapper.eq(AppUpdate::getAppType, Integer.valueOf(1));
            //苹果系统
        }else if(agent.contains(AppUpdateController.IPHONE)
                ||agent.contains(AppUpdateController.IPOD)
                ||agent.contains(AppUpdateController.IPAD)) {
            wrapper.eq(AppUpdate::getAppType, Integer.valueOf(0));
        }
        return R.ok(appUpdateService.getOne(wrapper));
    }

    /**
     * 新增app更新版本
     *
     * @param appUpdate app更新版本
     * @return R
     */
    @ApiOperation(value = "新增app更新版本", notes = "新增app更新版本")
    @SysLog("新增app更新版本")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_appupdate_add')")
    public R save(@RequestBody AppUpdate appUpdate) {
        return R.ok(appUpdateService.save(appUpdate));
    }

    /**
     * 修改app更新版本
     *
     * @param appUpdate app更新版本
     * @return R
     */
    @ApiOperation(value = "修改app更新版本", notes = "修改app更新版本")
    @SysLog("修改app更新版本")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_appupdate_edit')")
    public R updateById(@RequestBody AppUpdate appUpdate) {
        return R.ok(appUpdateService.updateById(appUpdate));
    }

    /**
     * 通过id删除app更新版本
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除app更新版本", notes = "通过id删除app更新版本")
    @SysLog("通过id删除app更新版本")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_appupdate_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(appUpdateService.removeById(id));
    }

}
